package com.ms.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.context.BaseContext;
import com.ms.domain.entity.Follow;
import com.ms.domain.entity.UserStats;
import com.ms.result.Result;
import com.ms.service.FollowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/follow")
@Tag(name = "C端用户关注相关接口")
@RequiredArgsConstructor
public class FollowController {

    private final FollowService followService;

    /**
     * 关注用户
     * @param followingId 被关注者ID
     * @return 关注结果
     */
    @PostMapping("/{followingId}")
    @Operation(summary = "关注用户接口")
    public Result<Void> followUser(@PathVariable Integer followingId) {
        Integer followerId = BaseContext.getCurrentId();
        log.info("用户 {} 准备关注用户 {}", followerId, followingId);
        if (followerId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            followService.followUser(followerId, followingId);
            return Result.success();
        } catch (Exception e) {
            return Result.badRequest(e.getMessage());
        }
    }

    /**
     * 取消关注用户
     * @param followingId 被关注者ID
     * @return 取消关注结果
     */
    @DeleteMapping("/{followingId}")
    @Operation(summary = "取消关注用户接口")
    public Result<Void> unfollowUser(@PathVariable Integer followingId) {
        Integer followerId = BaseContext.getCurrentId();
        if (followerId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            followService.unfollowUser(followerId, followingId);
            return Result.success();
        } catch (Exception e) {
            return Result.badRequest(e.getMessage());
        }
    }

    /**
     * 检查是否已关注用户
     * @param followingId 被关注者ID
     * @return 是否已关注
     */
    @GetMapping("/check/{followingId}")
    @Operation(summary = "检查是否已关注用户接口")
    public Result<Boolean> isFollowing(@PathVariable Integer followingId) {
        Integer followerId = BaseContext.getCurrentId();
        if (followerId == null) {
            return Result.unauthorized("请先登录");
        }

        boolean following = followService.isFollowing(followerId, followingId);
        return Result.success(following);
    }

    /**
     * 获取用户的关注列表
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页数量
     * @return 关注列表
     */
    @GetMapping("/following/{userId}")
    @Operation(summary = "获取用户关注列表接口")
    public Result<Page<Follow>> getFollowingList(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<Follow> followingList = followService.getFollowingList(userId, page, size);
            return Result.success(followingList);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户的粉丝列表
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页数量
     * @return 粉丝列表
     */
    @GetMapping("/followers/{userId}")
    @Operation(summary = "获取用户粉丝列表接口")
    public Result<Page<Follow>> getFollowersList(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<Follow> followersList = followService.getFollowersList(userId, page, size);
            return Result.success(followersList);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户统计信息
     * @param userId 用户ID
     * @return 用户统计信息
     */
    @GetMapping("/stats/{userId}")
    @Operation(summary = "获取用户统计信息接口")
    public Result<UserStats> getUserStats(@PathVariable Integer userId) {
        try {
            UserStats userStats = followService.getUserStats(userId);
            return Result.success(userStats);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}